package com.yike.user.controller;

import com.yike.common.tool.LoginUtil;
import com.yike.common.vo.LoginUserInfoVo;
import com.yike.user.param.PlatformUserLoginParam;
import com.yike.user.param.PlatformUserParam;
import com.yike.user.service.PlatformUserService;
import com.yike.user.vo.PlatformUserVo;
import lombok.extern.slf4j.Slf4j;
import com.yike.user.param.PlatformUserPageParam;
import com.yike.common.base.BaseController;
import com.yike.common.api.ApiResult;
import com.yike.common.pagination.Paging;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import com.yike.common.api.Add;
import com.yike.common.api.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 平台用户 控制器
 *
 * @author Sakura
 * @since 2024-10-29
 */
@Slf4j
@RestController
@RequestMapping("/platform/user")
@Module("user")
@Api(value = "平台用户API", tags = {"平台员工管理"})
public class PlatformUserController extends BaseController {

    @Autowired
    private PlatformUserService platformUserService;

    /**
     * 添加平台用户
     */
    @PostMapping("/add")
    @OperationLog(name = "新增员工", type = OperationLogType.ADD)
    @ApiOperation(value = "新增员工", response = ApiResult.class)
    public ApiResult<Boolean> addPlatformUser(@Validated(Add.class) @RequestBody PlatformUserParam platformUserParam) throws Exception {
        boolean flag = platformUserService.savePlatformUser(platformUserParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改平台用户
     */
    @PostMapping("/update")
    @OperationLog(name = "修改员工", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改员工", response = ApiResult.class)
    public ApiResult<Boolean> updatePlatformUser(@Validated(Update.class) @RequestBody PlatformUserParam platformUserParam) throws Exception {
        boolean flag = platformUserService.updatePlatformUser(platformUserParam);
        return ApiResult.result(flag);
    }

    /**
     * 删除平台用户
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除员工", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除员工", response = ApiResult.class)
    public ApiResult<Boolean> deletePlatformUser(@PathVariable("id") Long id) throws Exception {
        boolean flag = platformUserService.deletePlatformUser(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取平台用户详情
     */
    @GetMapping("/getPlatformUser/{id}")
    //@OperationLog(name = "平台用户详情", type = OperationLogType.INFO)
    @ApiOperation(value = "员工详情", response = PlatformUserVo.class)
    public ApiResult<PlatformUserVo> getPlatformUser(@PathVariable("id") Long id) throws Exception {
        PlatformUserVo platformUser = platformUserService.getPlatformUser(id);
        return ApiResult.ok(platformUser);
    }

    @GetMapping("/info")
    //@OperationLog(name = "登录用户详情", type = OperationLogType.INFO)
    @ApiOperation(value = "登录员工详情", response = PlatformUserVo.class)
    public ApiResult<PlatformUserVo> getPlatformUserInfo() throws Exception {
        PlatformUserVo platformUser = platformUserService.getPlatformUser(LoginUtil.getUserId());
        return ApiResult.ok(platformUser);
    }

    /**
     * 平台用户分页列表
     */
    @PostMapping("/getPageList")
    //@OperationLog(name = "平台用户分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "员工分页列表", response = PlatformUserVo.class)
    public ApiResult<Paging<PlatformUserVo>> getPlatformUserPageList(@Validated @RequestBody PlatformUserPageParam platformUserPageParam) throws Exception {
        Paging<PlatformUserVo> paging = platformUserService.getPlatformUserPageList(platformUserPageParam);
        return ApiResult.ok(paging);
    }

    @PostMapping("/login")
    @OperationLog(name = "登录", type = OperationLogType.ADD)
    @ApiOperation(value = "登录", response = LoginUserInfoVo.class)
    public ApiResult<LoginUserInfoVo> login(@RequestBody PlatformUserLoginParam platformUserLoginParam) throws Exception {
        LoginUserInfoVo loginUserInfoVo = platformUserService.login(platformUserLoginParam);
        return ApiResult.ok(loginUserInfoVo);
    }

}

